<?php

class Modules_Blogs extends Module {

	public $name = 'blogs';
	public $title = "Блоги";

	public static function subMenu() {
		return array(
			array(
				'icon' => '/tpl/admin/images/blogs-stack.png',
				'url' => '/admin.php?module=blogs&action=blogs',
				'title' => 'Список блогов',
			)
		);
	}

	public function actions() {
		return array(
			'blog' => 'Показывать блог'
		);
	}

	public function mainActions() {
		return array(
			'blog' => 'Показывать блог'
		);
	}

	protected function _actionproperties(Blocks $block) {
		switch ($block->action) {
			case 'blog':
				return $this->_blogs_list($block);
				break;

			default:
				return "Нет";
				break;
		}
	}

	public function adminActionIndex() {
		Request::redirect("/admin.php?module=blogs&action=blogs");
	}

	public function adminActionBlogs() {
		$this->access("r");
		
		$blogs = Doctrine_Query::create()
						/*->select('b.*, COUNT(p.id) AS posts')
						->from('Blogs b, Users u')
						->leftJoin('Blogs.Posts p')
						->where('Blogs.owner = u.id')
						->groupBy('Blogs.id')*/
						->from('Blogs b')
						->execute();

		$this->tpl()->assign(array(
			'blogs' => $blogs
		));
		return $this->tpl()->fetch('blogs_list.html');
	}

	public function adminActionBlog_create() {
		$this->access("c");
		
		$blog = new Blogs();
		$blog->date_createtime = time();

		return $this->_edit_blog($blog, 'blog_create.html');
	}

	public function adminActionBlog_edit() {
		$this->access("c");
		
		$id = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : 0;
		if (!$id) Request::redirect(404);
		$blog = BlogsTable::getInstance()->find($id);
		if (!$blog) Request::redirect(404);

		return $this->_edit_blog($blog, 'blog_edit.html');
	}

	public function adminActionBlog_active() {
		$this->access("p");
		
		$blog = $this->_load_blog();

		$blog->active = 1;
		$blog->save();

		Request::redirect("/admin.php?module=blogs");
	}

	public function adminActionBlog_unactive() {
		$this->access("p");
		
		$blog = $this->_load_blog();

		$blog->active = 0;
		$blog->save();

		Request::redirect("/admin.php?module=blogs");
	}

	public function adminActionBlog_save() {
		$this->access("c");
		
		$blogData = isset($_REQUEST['blog']) ? $_REQUEST['blog'] : array();
		$blogData['id'] = (int) $blogData['id'];
		$blogData['date_createtime'] = strtotime($blogData['date_createtime']);

		$blog = $blogData['id'] ? BlogsTable::getInstance()->find($blogData['id']) : new Blogs();

		unset($blogData['id']);
		$blog->fromArray($blogData);

		$blog->save();

		Request::redirect(isset($_REQUEST['apply']) ? "/admin.php?module=blogs&action=blog_edit&id={$blog->id}" : "/admin.php?module=blogs");
	}

	public function adminActionPost_list() {
		$this->access("r");
		
		$blog = $this->_load_blog(true, true);

		$this->tpl()->assign(array(
			'blog' => $blog,
		));
		return $this->tpl()->fetch('post_list.html');
	}

	public function adminActionPost_create() {
		$this->access("c");
		
		$blog = $this->_load_blog();

		$post = new BlogPosts();
		$post->date_createtime = time();
		$post->author = $blog->owner;

		return $this->_edit_post($post, $blog, 'post_create.html');
	}

	public function adminActionPost_edit() {
		$this->access("c");
		
		$post = $this->_load_post(true);

		$post->text = !empty($post->text) ? ($post->preview . '<!-- pagebreak -->' . $post->text) : $post->preview;

		return $this->_edit_post($post, $post->Blog, 'post_edit.html');
	}

	public function adminActionPost_active() {
		$this->access("p");
		
		$post = $this->_load_post();

		$post->active = 1;
		$post->save();

		Request::redirect("/admin.php?module=blogs&action=post_list&blog={$post->blog_id}");
	}

	public function adminActionPost_unactive() {
		$this->access("p");
		
		$post = $this->_load_post();

		$post->active = 0;
		$post->save();

		Request::redirect("/admin.php?module=blogs&action=post_list&blog={$post->blog_id}");
	}

	public function adminActionPost_save() {
		$this->access("c");
		
		$postData = isset($_REQUEST['post']) ? $_REQUEST['post'] : array();
		$postData['id'] = (int) $postData['id'];

		$post = $postData['id'] ? BlogPostsTable::getInstance()->find($postData['id']) : new BlogPosts();

		$postData['date_createtime'] = strtotime($postData['date_createtime']);
		$text = explode('<!-- pagebreak -->', $postData['text']);
		$postData['preview'] = $text[0];
		$postData['text'] = isset($text[1]) ? $text[1] : "";

		unset($postData['id']);
		$post->fromArray($postData);

		$post->save();

		Request::redirect(isset($_REQUEST['apply']) ? "/admin.php?module=blogs&action=post_edit&id={$post->id}" : "/admin.php?module=blogs&action=post_list&blog={$post->blog_id}");
	}

	public function adminActionPost_view() {
		$this->access("r");
		
		$post = $this->_load_post(true, true);

		App::registerCoreScript('jquery');
		App::registerScript('/inc/Modules/Blogs/js/view_post.js');

		$users = Doctrine_Query::create()
						->from("Users u")
						->execute();

		$this->tpl()->assign(array(
			'post' => $post,
			'blog' => $post->Blog,
			'users' => $users,
			'comments' => $post->comments(true),
		));
		return $this->tpl()->fetch('post_view.html');
	}

	public function adminActionComment_delete() {
		$this->access("d");
		
		$comment = $this->_load_comment();
		$row = $comment->delete();
		return $row;
	}

	public function adminActionComment_edit() {
		$this->access("c");
		
		$comment = $this->_load_comment();

		$users = Doctrine_Query::create()
						->from("Users u")
						->execute();

		$this->tpl()->assign(array(
			'comment' => $comment,
			'post' => $comment->Post,
			'users' => $users
		));
		return $this->tpl()->fetch('_comment_form.html');
	}

	public function adminActionComment_create() {
		$this->access("c");
		
		$answer_to = isset($_REQUEST['answer_to']) ? $_REQUEST['answer_to'] : null;
		$comment = new BlogComments();
		$comment->answer_to = $answer_to;
		$users = Doctrine_Query::create()
						->from("Users u")
						->execute();
		$this->tpl()->assign(array(
			'comment' => $comment,
			'post' => $comment->Answer->Post,
			'users' => $users,
		));
		return $this->tpl()->fetch('_comment_form.html');
	}

	public function adminActionComment_save() {
		$this->access("c");
		
		$commentData = isset($_REQUEST['comment']) ? $_REQUEST['comment'] : array();
		$commentData['id'] = (int) $commentData['id'];
		$commentData['date_createtime'] = strtotime($commentData['date_createtime']);

		$comment = $commentData['id'] ? BlogCommentsTable::getInstance()->find($commentData['id']) : new BlogComments();
		if (!$commentData['answer_to']) unset($commentData['answer_to']);
		unset($commentData['id']);

		$comment->fromArray($commentData);
		$comment->save();

		$this->tpl()->assign(array(
			'comment' => $comment
		));
		return $this->tpl()->fetch('_comment.html');
	}

	public function adminActionUser_blogs() {
		$this->access("r");
		
		$user = isset($_REQUEST['user']) ? $_REQUEST['user'] : 0;
		if (!$user) return "";

		$user = Doctrine_Query::create()
						->from("Users u")
						->leftJoin('u.Blogs')
						->where('u.id = ?', $user)
						->fetchOne();
		if (!$user) return "";

		$this->tpl()->assign(array(
			'user' => $user,
		));
		return $this->tpl()->fetch('user_blog.html');
	}

	public function actionBlog() {
		$blog_id = $this->block->property('blog')->value;
		if (!$blog_id) return "";
		$blog = Doctrine_Query::create()
						->from("Blogs b")
						->leftJoin("b.Owner o")
						->leftJoin("b.Posts p")
						->leftJoin("o.Blogs")
						->where('b.id = ?', $blog_id)
						->fetchOne();
		if (!$blog) Request::redirect(404);

		$this->tpl()->assign(array(
			'blog' => $blog,
		));
		return $this->tpl()->fetch($this->getTpl('blog.html'));
	}

	private function _load_comment() {
		$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : 0;
		if (!$id) Request::redirect(404);
		$comment = BlogCommentsTable::getInstance()->find($id);
		if (!$comment) Request::redirect(404);

		return $comment;
	}

	/**
	 *
	 * @param bool $blog
	 * @param bool $coments
	 * @return BlogPosts
	 */
	private function _load_post($blog = false, $coments = false) {
		$id = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : 0;
		if (!$id) Request::redirect(404);

		$q = Doctrine_Query::create()
						->from("BlogPosts p")
						->where('p.id = ?', $id);
		if ($blog) $q->leftJoin('p.Blog b');
		if ($coments) {
			$q->leftJoin('p.Comments c');
			$q->leftJoin('c.Author ca');
		}
		$post = $q->fetchOne();
		if (!$post) Request::redirect(404);

		return $post;
	}

	private function _load_blog($posts = false, $comments = false) {
		$id = isset($_REQUEST['blog']) ? (int) $_REQUEST['blog'] : 0;
		if (!$id) Request::redirect(404);

		$blog = Doctrine_Query::create()
						->from("Blogs b")
						->where('b.id = ?', $id);
		if ($posts) $blog->leftJoin('b.Posts p');
		if ($comments) $blog->leftJoin('p.Comments c');

		$blog = $blog->fetchOne();
		if (!$blog) Request::redirect(404);

		return $blog;
	}

	private function _edit_post($post, $blog, $tpl) {
		App::registerCoreScript('jquery');
		App::registerCoreScript('tiny_mce');
		App::registerScript('/js/tiny_mce_init.js');

		$users = Doctrine_Query::create()
						->from("Users u")
						->execute();

		$this->tpl()->assign(array(
			'post' => $post,
			'users' => $users,
			'blog' => $blog,
		));
		return $this->tpl()->fetch($tpl);
	}

	private function _edit_blog($blog, $tpl) {
		$users = Doctrine_Query::create()
						->from("Users u")
						->execute();

		$this->tpl()->assign(array(
			'blog' => $blog,
			'users' => $users,
		));
		return $this->tpl()->fetch($tpl);
	}

	private function _blogs_list($block) {
		$users = Doctrine_Query::create()
						->from("Users u")
						->leftJoin('u.Blogs')
						->execute();
		$blog = $block->property('blog');
		if ($blog) $blog = Doctrine_Query::create ()
							->from('Blogs b')
							->where('b.id = ?', $blog->value)
							->fetchOne();
		$this->tpl()->assign(array(
			'users' => $users,
			'block' => $block,
			'blog' => $blog,
		));
		return $this->tpl()->fetch('aprp_blogs_list.html');
	}

}